home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group96a.txt / 000094_icon-group-sender_Wed Apr 17 12:48 MST 1996.msg < prev    next >
Text File  |  1996-09-05  |  2KB  |  68 lines

  1.     Date: Wed, 17 Apr 1996 17:36:13 +0100
  2.     From: Hamish Lawson <H.Lawson@tees.ac.uk>
  3.     Mime-Version: 1.0
  4.     To: icon-group@cs.arizona.edu
  5.     Subject: Splitting and joining strings
  6.     Content-Transfer-Encoding: 7bit
  7.  
  8.     Before I rolled my own I thought I'd have a look in the Icon Program
  9.     Library for a couple of procedures I need.
  10.  
  11.     The first should split a string on a specified separator, along the
  12.     lines of
  13.  
  14.        split("Joe:Sue:Bob:Ann", ":") produces "Joe", "Sue", "Bob", "Ann"
  15.  
  16.     The second should join the elements of a list into a string, with each
  17.     element separated by a specified separator, along the lines of:
  18.      
  19.        join(["Joe", "Sue", "Bob", "Ann"], ":") produces "Joe:Sue:Bob:Ann"
  20.  
  21.     I couldn't find any such procedures in the IPL. Have I missed them?
  22.  
  23. Here are two routines that I've got: split and atos (aggregate to string).
  24.  
  25. #
  26. # split(s, delimiters, keepall) splits the string s into consecutive substrings
  27. #  that do/do not consist of characters in the cset delimiters, producing a
  28. #  list of strings.  If keepall is null, strings consisting delimiters are not
  29. #  included in the result list.
  30. #
  31. # If not specified, delimeters defaults to blank and tab, which essentially
  32. # "tokenizes" non-whitespace:
  33. #
  34. #   words := split(read())
  35. #
  36. procedure split(s, dlms, keepall)
  37.    local w, ws, addproc, nullproc
  38.  
  39.    ws := []
  40.    /dlms := " \t"
  41.    
  42.    addproc := put
  43.    if \keepall then
  44.       otherproc := put
  45.    else
  46.       otherproc := 1
  47.    
  48.    if dlms := (any(dlms, s[1]) & ~dlms) then
  49.       otherproc :=: addproc
  50.     
  51.    s ? while w := tab(many(dlms := ~dlms)) do {
  52.       addproc(ws, w)
  53.       otherproc :=: addproc
  54.       }
  55.  
  56.    return ws
  57. end
  58.  
  59.  
  60. procedure atos(a, delim)
  61.     local e, s
  62.     /delim := ","
  63.     every e := !a do
  64.         (/s := e) | (s ||:= delim || e)
  65.     return s
  66. end
  67.  
  68.